package com.mshen.service.model;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 暂时没有用到这个类
 * @author liushd
 *
 */
public class MessageCache {
	private static MessageCache instance=
			new MessageCache();
	private MessageCache(){
		
	}
	public static MessageCache getInstance(){
		return instance;
	}
	private List<String> list=new 
			LinkedList<String>();
	private Map<String,String> map=new HashMap<String,
			String>();
			
	
	private int maxsize=2000;//默认支持2000个并发
	public int getSize(){
		return list.size();
	}
	public int getMaxsize() {
		return maxsize;
	}
	public void setMaxsize(int maxsize) {
		this.maxsize = maxsize;
	}
	/**
	 * 取值
	 * @param key
	 * @return
	 */
	public String get(String key){
		return map.get(key);
	}
	/**
	 * 设置缓存
	 * @param key
	 * @param value
	 */
	public synchronized void put(String key,String value){
		if(map.size()>=maxsize){
			String firstkey=list.remove(0);
			System.out.println("firstkey:"+firstkey);
			map.remove(firstkey);
			list.add(key);
			map.put(key, value);
		}else{
			list.add(key);
			map.put(key, value);
		}
	}
	
	public static void main(String[] args) {
		MessageCache msgcache=new MessageCache();
		msgcache.setMaxsize(2);
		msgcache.put("0", "0");
		msgcache.put("31", "31");
		msgcache.put("22", "22");
		msgcache.put("11", "11");
		System.out.println("大小:"+msgcache.getSize());
		
	}
	
}
